<script>
  import {
    parseKeyValueString
  } from '@/common/utils/util.js'
  import routingIntercept from '@/common/utils/permission.js'
  import {getClosePsychologicalUrl,getDynamicSettings} from '@/common/api/scl_api.js'
  import {getConfigDict} from '@/common/api/staff.js'
  
  export default {
    globalData: {
      topHeight: null
    },
    onLaunch: function(opt) {
      console.log('onlaunch：', opt)
      // r =  1 - 查看订单详情；2 - 订单列表；3 - 支付页； 4 - 评价订单页；5 - 测评体检包页
      try{
        
        let clickId = opt.query.qz_gdt || opt.query.gdt_vid || opt.query.click_id
        if(clickId){
          uni.setStorageSync('adClickId',clickId)
        }
        
        if(opt.query.userType == 'e-commerce'){
          uni.setStorageSync('isFromEshopLink',true)
        }else{
          uni.removeStorageSync('isFromEshopLink')
        }
      }catch(e){
        uni.removeStorageSync('isFromEshopLink')
        //TODO handle the exception
      }
      
      routingIntercept()// 对路由进行统一拦截，实现路由导航守卫 router.beforeEach 功能
      this.setGlobalData()
    },
    onShow: async function(opt) {
      console.log('show：', opt)
    
      this.checkSclActivitySwitch()
      this.$store.dispatch('user/checkFirstSclIsEshop')
      await this.$store.dispatch('app/getAppDictConfig')
      
      if(opt.query && opt.query.adType == '1'){
        this.$store.commit("user/setAdType",opt.query.adType)
      }
      uni.removeStorageSync('from')  
      this.setScanParams(opt)
      this.setShareData()
      this.getAppConfig()
      this.checkUpdate()
    },
    methods: {
      checkUpdate(){
        if (uni.getUpdateManager) {
          const updateManager = uni.getUpdateManager();
          updateManager.onCheckForUpdate(function(res) {
            // 请求完新版本信息的回调
            // console.log(res.hasUpdate);
          });
          updateManager.onUpdateReady(function(res) {
            uni.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                }
              }
            });
          });
          updateManager.onUpdateFailed(function(res) {
            // 新的版本下载失败
            uni.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
              showCancel: false
            });
          });
        }
      },
      async getAppConfig(){
        const {data:dRes} = await getConfigDict()
        if(dRes.success){
          console.log(dRes,'----dRes vgetAppConfig')
          this.$store.commit('app/setDict',dRes.data)
        }else{
          console.log('获取配置失败')
        }
      },
      async checkSclActivitySwitch(){
        try{
          const {data:dRes} = await getClosePsychologicalUrl()
          if(dRes.success){
            this.$store.commit("app/setActivitySwitch",dRes.data)
          }
          
          const {data:fRes} = await getDynamicSettings()
          if(fRes.success){
            this.$store.commit('app/setSclFreeMenuSwitch',fRes.data.sclFreeMenu == 'Y')
          }
        }catch{
          
        }
      },
      setShareData() {
        let menus = []
        if (uni.getSystemInfoSync().uniPlatform == 'mp-toutiao') {
          menus = ["share"]
        } else {
          menus = ["shareAppMessage", "shareTimeline"]
        }
        
        uni.showShareMenu({
          withShareTicket: true,
          menus
        })
      },
      setGlobalData() {
        const that = this
        uni.getSystemInfo({
          success(res) {
            console.log('getSystemInfo：',res)
            
            const {model,statusBarHeight,windowHeight,screenHeight} = res
            let heightVar = 0
            
            if (model.indexOf('iPhone') !== -1) {
              if (model.includes("iPhone 5")) {
                heightVar = 56
              } else if (model.includes("iPhone XR") || model.includes("iPhone XS Max")) {
                heightVar = 36
              } else if (model.includes("Plus")) {
                heightVar = 40
              } else {
                heightVar = 44
              }
            } else {
              // android
              heightVar = 48
            }
            							
            that.globalData.topHeight = statusBarHeight + heightVar
            that.globalData.windowHeight = windowHeight
            that.globalData.screenHeight = screenHeight
          }
        })
      },
      setScanParams(opt) {    
        const queryData = opt.query?.scene ? parseKeyValueString(decodeURIComponent(opt.query.scene)) :opt.query
        
        console.log(queryData,'---queryData--')
        if(!queryData.r && !queryData.p){
          //不存在模式标识则不执行下方流程  
          return false
        }
        
        if(queryData.r == '5'){
          uni.setStorageSync('inviteId',queryData.id)
          console.log(queryData.id,'-------queryData.id=====inviteId')
          return false
        }else if(queryData.p){
          console.log('需解析后才能获知所需数据跳转路径：',queryData)
          return false
        }
        
        
        console.log('setScanParams - queryData：',queryData)
        let mode=''
        let postKey=''
        let commonId=''
        
        if (queryData.r == '2') {
          //1 - 'pages/order/order'
          mode = 'info'
          postKey = 'userId'
          commonId = queryData.uid
        }else if (queryData.r == '1') {
          //2 - 'pages/order/detail'
          if (queryData.oid) {
            mode = 'order'
            postKey = 'orderId'
            commonId = queryData.oid
          } else {
            mode = 'booking'
            postKey = 'userId'
            commonId = queryData.uid
          }
        } else if (queryData.r == '3') {
          //3 - 'subcontractorA/payment/details'
          mode = 'pay'
          postKey = 'payId'
          commonId = queryData.uid
        } else if (queryData.r == '4'){
          //4 - 'pages/order/comment' 去评价订单
          mode = 'comment'
          postKey = 'orderId'
          commonId = queryData.oid||queryData.orderUid
        }

        this.$store.commit("app/setScanParams",{
          mode,
          postKey,
          commonId
        }) 
        
      }
      
    }
  }
</script>

<style>
  /*每个页面公共css */
  @import url("./common/css/global.css");
  @import url("./common/css/globalPop.css");
  @import url("./common/css/globalConsultingList.css");
  
  .discount-modal-a .row-4 .u-count-down__text{
    font-size: 24rpx !important;
    color: #666666 !important;
  }
  .discount-tip-position .discount-tip .u-count-down__text{
    font-size: 24rpx !important;
    color: #fff !important;
  }
  
  .discount-modal-b-wrapper .coupon-box-tip .u-count-down__text{
    font-size: 22rpx;
    font-family: PingFang-SC-Medium, PingFang-SC;
    font-weight: 500;
    color: #CAB0B0;
    line-height: 23px;
  }
  
  
  /* ::-webkit-scrollbar
  {
      width: 16rpx!important;
      height: 16rpx!important;
      background-color: #F5F5F5;
  } */
    
  /*定义滚动条轨道 内阴影+圆角*/
  /* ::-webkit-scrollbar-track
  {
      // -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
      border-radius: 10px;
      background-color: #fff;
  } */
  
  .scroll-y-view ::-webkit-scrollbar {
  	display: block !important;
  	width: 6rpx !important;
  }
    
  /*定义滑块 内阴影+圆角*/
 ::-webkit-scrollbar-thumb
  {
      border-radius: 6rpx;
      -webkit-box-shadow: inset 0 0 4rpx rgba(0,0,0,.3);
      background-color: #f5f6f6;
  }
      
</style>